package com.zhanghu.backendZray.converter;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author ZhangDong
 * @date 2024/7/20 11:39
 */
public abstract class Converter<I, D, O> {

    private final Function<I, D> input2Entity;
    private final Function<D, O> entity2Vo;
    protected Converter(final Function<I, D> input2Entity, final Function<D, O> entity2Vo) {
        this.input2Entity = input2Entity;
        this.entity2Vo = entity2Vo;
    };

    public final D input2Entity(I input) {
        return input2Entity.apply(input);
    }

    public final O entity2Vo(D d) {
        return entity2Vo.apply(d);
    }

    public final List<D> input2Entitys(final List<I> inputs) {
        return inputs.stream().map(this::input2Entity).collect(Collectors.toList());
    }

    public final List<O> entity2Vos(final List<D> entities) {
        return entities.stream().map(this::entity2Vo).collect(Collectors.toList());
    }
}
